import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Edit implements Command {

	private ArrayList<Task> tasks;
	private AddParser addParser;
	private Storage storage;
	private History history;
	private Logger logger;
	private String data;
	private String errorMessage;
	private String output;
	private boolean isEditing;
	private boolean isError;

	private static final String EDIT_SUCCESSFUL = "Task edited successfully\n";
	private static final String EDIT_UNSUCCESSFUL = "Task edited unsuccessfully\n";
	private static final String COMMAND_EDIT = "edit";
	private static final int BASEINDEX = 1;

	public Edit(ArrayList<Task> tasks, Storage storage, History history,
			Logger logger,boolean isEditing) {
		this.tasks = tasks;
		this.addParser = new AddParser();
		this.storage = storage;
		this.history = history;
		this.logger = logger;
		this.isEditing = isEditing;
		errorMessage = "";
	}

	@Override
	public void acceptInput(String data) {
		// TODO Auto-generated method stub
		this.data = data;

	}

	@Override
	public void execute() {
		// TODO Auto-generated method stub
		int id;
		Task modifiedTask;

		id = getTaskID(data);
		if (isIDValid(id)) {
			id = id - BASEINDEX;
			data = getTaskContent(data);
			try {
				modifiedTask = addParser.parse(data, tasks);
			} catch (ParseException e) {
				errorMessage = e.getMessage();
				logger.log(Level.INFO, errorMessage);
				return;

			}
			history.storeUndo(tasks, COMMAND_EDIT);
			tasks.set(id, modifiedTask);

			// save();
			output = EDIT_SUCCESSFUL;
		} else {
			output = EDIT_UNSUCCESSFUL;
		}
	}

	@Override
	public String save() throws IOException {
		// TODO Auto-generated method stub
		return storage.writeToStorage(tasks);
	}

	@Override
	public String outputToUser() {
		// TODO Auto-generated method stub
		if (!errorMessage.equals("")) {
			output = errorMessage;
		}
		return output;
	}

	// check if the original ID given by the user is valid. NOTE: ID is not
	// adjusted to base 0 of ArrayList
	private boolean isIDValid(int id) {

		if (tasks.size() >= 1) {
			if (id >= 1 && id <= tasks.size()) {
				return true;
			}
		}
		return false;
	}

	private int getTaskID(String data) {
		return Integer.valueOf(obtainFirstWord(data));
	}

	private String obtainFirstWord(String sentence) {

		String[] words;
		String trimmedSentence;
		trimmedSentence = sentence.trim();

		words = trimmedSentence.split(" ");

		return words[0];

	}

	private String getTaskContent(String data) {
		return omitFirstWord(data);
	}

	private String omitFirstWord(String sentence) {

		String[] words;
		String trimmedSentence;
		trimmedSentence = sentence.trim();

		words = trimmedSentence.split(" ", 2);
		if (words.length == 2) {
			return words[1];
		} else {
			return "";
		}

	}

	@Override
	public boolean isError() {
		// TODO Auto-generated method stub
		return isError;
	}

}
